<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "calc((100vh - 200px) / 3)",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    color: {
      type: String,
      default: "#465E7A",
    },
    baseDataParam: {
      type: Object,
      default: () => {},
    },
    title: {
      type: String,
      default: "电能质量分析",
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {},
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart(params) {
      this.chart = echarts.init(this.$el, "macarons");
      this.chart.setOption({
        title: {
          text: params.title.split("").join("\n"),
          left: "1%",
          top: "center", // 标题水平居中
        },
        tooltip: {
          trigger: "axis", // 当鼠标悬停时显示所有折线的对应值
        },
        grid: {
          left: "5%", // 距离左边的距离
          right: "5%", // 距离右边的距离
          top: "10%", // 距离顶部的距离
          bottom: "10%", // 距离底部的距离
        },
        toolbox: {
          feature: {
            // saveAsImage: {}, // 保存为图片
            // dataView: { readOnly: false }, // 数据视图
            magicType: {
              // 动态类型切换
              type: ["line", "bar"],
            },
            // restore: {} // 还原
          },
          right: 100,
        },
        legend: {
          data: ["折线1", "折线2", "折线3"], // 图例，用于区分不同的折线
        },
        xAxis: {
          type: "category",
          data: params.timeDate,
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            name: params.title, // 折线名称,
            type: "line",
            data: params.data, // 折线数据
            itemStyle: {
              color: this.color, // 设置数据点颜色为绿色
            },
            lineStyle: {
              color: this.color, // 设置折线颜色
            },
          },
        ],
      });
      // this.chart.hideLoading();
      // getFlowLineData(this.baseDataParam).then((res) => {
      //   console.info(res);
      // });
    },
  },
};
</script>
